'''
Created on Jun 12, 2010

@author: oabalbin
'''
## species_ncbi_taxon_id ## protein_id ## alias ## source ##

import os
import glob
from collections import deque, defaultdict


def parse_STRING_protein_alias(inputfile, outputfile):
    """
    It reads the protein aliases table from string and filter out onnly the human data
    ## species_ncbi_taxon_id ## protein_id ## alias ## source ## 
    """
    for line in inputfile: 
        line = line.strip('\n')
        fields = line.split('\t')
        # To skip headers, star reading samples in column 7 of the file
        if fields[0] == '#': 
            continue
        if fields[0] == '9606':
            fields = sum([[fields[0]], ['9606.'+fields[1]], fields[2:]],[])
            outputfile.write(",".join(fields).replace(',','\t')+'\n')
    
            
def parse_STRING_protein_actions(inputfile, outputfile):
    """
    item_id_a       item_id_b       mode    action  a_is_acting     score
    """
    for line in inputfile: 
        line = line.strip('\n')
        fields = line.split('\t')
        # To skip headers, star reading samples in column 7 of the file
        if fields[0] == '#': 
            continue
        if fields[0].startswith('9606.'):
            outputfile.write(",".join(fields).replace(',','\t')+'\n')
            
    


def parse_STRING_protein_actions_detailed(inputfile, outputfile):
    """
    item_id_a       item_id_b       mode    action  a_is_acting     score   sources transferred_sources
    """
    for line in inputfile: 
        line = line.strip('\n')
        fields = line.split('\t')
        # To skip headers, star reading samples in column 7 of the file
        if fields[0] == '#': 
            continue
        if fields[0].startswith('9606.'):
            outputfile.write(",".join(fields).replace(',','\t')+'\n')
        

def parse_STRING_protein_links_detailed(inputfile, outputfile):
    """
    protein1 protein2 neighborhood fusion cooccurence coexpression experimental database textmining combined_score
    """
    for line in inputfile: 
        line = line.strip('\n')
        #fields = line.split('\t')
        fields = line.split(' ')
        # To skip headers, star reading samples in column 7 of the file
        if fields[0] == '#': 
            continue
        if fields[0].startswith('9606.'):
            outputfile.write(",".join(fields).replace(',','\t')+'\n')

def parse_STRING_protein_links(inputfile, outputfile):
    """
    It parses protein links file for human only
    """
    for line in inputfile: 
        line = line.strip('\n')
        fields = line.split(' ')
        # To skip headers, star reading samples in column 7 of the file
        if fields[0] == '#': 
            continue
        if fields[0].startswith('9606.'):
            outputfile.write(",".join(fields).replace(',','\t')+'\n')

def dict_of_names(inputfile):
    """
    Reads a file with a list of hugo gene Names and an associated value
    """    
    genlist=defaultdict()
    for line in inputfile:        
        line = line.strip('\n')
        fields = line.split('\t')
        #print fields 
        # To skip headers, star reading samples in column 7 of the file
        if fields[0][0] == '#': 
            continue                             
        if fields[0]=='':
            continue
        
        genlist[fields[0]] = fields[1]
    
    return genlist
    


